﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Services;
using AjaxControlToolkit;
using Entities;
using Logic;

namespace VotanetWebSite.WS
{
    /// <summary>
    /// Descripción breve de WSRegionComunne
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
    [System.Web.Script.Services.ScriptService]
    public class WSRegionComunne : System.Web.Services.WebService
    {

        [WebMethod]
        public CascadingDropDownNameValue[] GetListRegions(string knownCategoryValues, string category)
        {
            RegionComunneLogic rcl = new RegionComunneLogic();
            List<Region> regionList = rcl.GetListRegionsLogic();

            List<CascadingDropDownNameValue> cascadaRegiones = new List<CascadingDropDownNameValue>();

            if (regionList != null)
            {
                foreach (Region rc in regionList)
                {
                    string regionId = rc.id.ToString();
                    string regionName = rc.name;
                    cascadaRegiones.Add(new CascadingDropDownNameValue(regionName, regionId));
                }

                return cascadaRegiones.ToArray();
            }
            else
            {
                return null;
            }
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetListCommunes(string knownCategoryValues, string category)
        {
            StringDictionary categoryValues = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            string[] categoriesArray = knownCategoryValues.Split(':', ';');
            int id = Convert.ToInt32(categoriesArray[1]);

            RegionComunneLogic rcl = new RegionComunneLogic();
            List<Commune> commune = rcl.GetListComunneLogic(id);
            if (commune != null)
            {
                List<AjaxControlToolkit.CascadingDropDownNameValue> communeCascading = new List<AjaxControlToolkit.CascadingDropDownNameValue>();

                foreach (Commune cc in commune)
                {
                    string comunneId = cc.id.ToString();
                    string comunneName = cc.name;
                    string comunne = new VoterLogic().FirstLetterToUppercaseLogica(comunneName.ToLower());
                    communeCascading.Add(new AjaxControlToolkit.CascadingDropDownNameValue(comunne, comunneId));
                }

                return communeCascading.ToArray();
            }
            else
            {
                return null;
            }
        }
    }
}
